package com.itic.mall.system.project.score.service;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.service.CrudService;
import com.itic.appbase.framework.type.ProjectScoreType;
import com.itic.appbase.framework.utils.NumberStringHelper;
import com.itic.mall.system.inter.project.score.persistence.dao.ProjectScoreDao;
import com.itic.mall.system.inter.project.score.persistence.model.ProjectScore;
import com.itic.mall.system.inter.project.score.persistence.model.ProjectScoreExample;
import com.itic.mall.system.inter.project.score.persistence.model.ProjectScoreExample.Criteria;
import com.itic.mall.system.inter.users.usersmall.persistence.model.UsersMall;
import com.itic.mall.system.users.pointslog.service.UsersPointslogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Transactional
public class ProjectScoreService
     extends CrudService<ProjectScoreDao,ProjectScore,ProjectScoreExample> {
    @Autowired
    private ProjectScoreDao projectScoreDao;
    @Autowired
    private UsersPointslogService usersPointslogService;
    
    public ProjectScore getProjectScoreByType(ProjectScoreType type) {
    	ProjectScoreExample example = new ProjectScoreExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andIsEffectiveEqualTo(DBConst.TRUE);
    	criteria.andIsUseDeducEqualTo(1);
    	criteria.andTypeEqualTo(type.getType());
    	List<ProjectScore> projectScores = this.list(example);
    	if(projectScores!=null)
    		return projectScores.get(0);
    	else
    		return null;
    	
    }
    
    public Map<String, Double> calScoreRatio(double totalMoney) {
    	Map<String,Double> scoreMap = new HashMap<>();
    	ProjectScore useScoreType = this.getProjectScoreByType(ProjectScoreType.Use);
    	ProjectScore DeducScoreType = this.getProjectScoreByType(ProjectScoreType.Deduc);
    	
    	if(useScoreType!=null && DeducScoreType!=null) {
	    	double useCost = useScoreType.getCost();
	    	double useScore = useScoreType.getScore();
	    	BigDecimal useCostDecimal = new BigDecimal(useCost);
	    	BigDecimal useScoreDecimal = new BigDecimal(useScore);
	    	BigDecimal totalMoneyDecimal = new BigDecimal(totalMoney);
	    	BigDecimal useDecimal= totalMoneyDecimal.divide(useCostDecimal,0,BigDecimal.ROUND_HALF_UP);
	    	BigDecimal use =useDecimal.multiply(useScoreDecimal);
	    	BigDecimal deducCostDecimal = new BigDecimal(DeducScoreType.getCost());
	    	BigDecimal deducScoreDecimal = new BigDecimal(DeducScoreType.getScore());
	    	double deduc = use.divide(deducCostDecimal).multiply(deducScoreDecimal).doubleValue();
	    	scoreMap.put("use",use.doubleValue());
    		scoreMap.put("deduc",deduc);
    	}else {
    		scoreMap.put("use",0d);
    		scoreMap.put("deduc",0d);
    		return scoreMap;
    	}
    	return scoreMap;
    }
    
    public Map<String, Double> calUserScoreRatio(UsersMall usersMall,double totalMoney) {
    	Map<String,Double> scoreMap = new HashMap<>();
    	ProjectScore useScoreType = this.getProjectScoreByType(ProjectScoreType.Use);
    	ProjectScore DeducScoreType = this.getProjectScoreByType(ProjectScoreType.Deduc);
    	//查看user所剩余的惠金币
    	if(useScoreType!=null && DeducScoreType!=null && totalMoney>0) {
	    	double useCost = useScoreType.getCost();
	    	double useScore = useScoreType.getScore();
	    	BigDecimal useCostDecimal = new BigDecimal(useCost);
	    	BigDecimal useScoreDecimal = new BigDecimal(useScore);
	    	BigDecimal totalMoneyDecimal = new BigDecimal(totalMoney);
	    	BigDecimal useDecimal= totalMoneyDecimal.divide(useCostDecimal,0,BigDecimal.ROUND_HALF_UP);
	    	BigDecimal use =useDecimal.multiply(useScoreDecimal);
	    	double userScore = usersMall.getJifeng()==null?0 : usersMall.getJifeng();
	    	if(userScore < use.doubleValue())
	    		use = new BigDecimal(userScore);
	    	BigDecimal deducCostDecimal = new BigDecimal(DeducScoreType.getCost());
	    	BigDecimal deducScoreDecimal = new BigDecimal(DeducScoreType.getScore());
	    	double deduc = use.divide(deducCostDecimal).multiply(deducScoreDecimal).doubleValue();
	    	scoreMap.put("use",use.doubleValue());
    		scoreMap.put("deduc",deduc);
    	}else {
    		scoreMap.put("use",0d);
    		scoreMap.put("deduc",0d);
    		return scoreMap;
    	}
    	return scoreMap;
    }
    
    public int calGiveUserScore(double consume) {
    	ProjectScore projectScore = this.getProjectScoreByType(ProjectScoreType.Consume);
    	if(projectScore==null)
    		return 0;
    	double cost = projectScore.getCost()==null?1:projectScore.getCost();
    	int score = projectScore.getScore()==null?0:projectScore.getScore();
    	String scoreStr =NumberStringHelper.divString(Double.toString(consume), Double.toString(cost), 0);
    	scoreStr = NumberStringHelper.mulString(scoreStr, Integer.toString(score));
    	return Integer.parseInt(scoreStr); 
    }
}